package com.ly.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.servlet.http.HttpServletRequest;
import java.io.FileWriter;
import java.util.*;

@Controller
@Slf4j
@RequestMapping("thymeleaf")
public class ThymeleafController {

    /**
     * 跳转页面
     * @param model
     * @param request
     * @return
     */
    @GetMapping("toTeacher")
    public String toTeacher(Model model, HttpServletRequest request) {
        model.addAttribute("there","luyong");
        Date birthday = new Date();
        model.addAttribute("birthday",birthday);
        Integer sex = 0;
        model.addAttribute("sex",sex);
        Integer sex2 = 2;
        model.addAttribute("sex2",sex2);

        List<String> list = new ArrayList<>();
        list.add("JAVA");
        list.add("安卓");
        list.add("IOS");
        list.add("大数据");
        list.add("C++");
        model.addAttribute("list",list);
        Map<String,Object> map = new HashMap<>();
        map.put("name","luyong");
        map.put("sex","男");
        map.put("age",18);
        model.addAttribute("map",map);

        request.setAttribute("session","11111111111111111111111");
        request.getSession().setAttribute("userToken","qwqwqwqwqw12121212");
        return "teacher";
    }

    @Autowired
    private TemplateEngine templateEngine;//这是teymeleaf下的引擎，直接引用就可以

    /**
     * 生成静态化页面
     * @param model
     * @param request
     * @return
     */
    @GetMapping("generateHtml")
    @ResponseBody
    public String generateHtml(Model model, HttpServletRequest request) throws Exception{
        model.addAttribute("there","luyong");
        Date birthday = new Date();
        model.addAttribute("birthday",birthday);
        Integer sex = 0;
        model.addAttribute("sex",sex);
        Integer sex2 = 2;
        model.addAttribute("sex2",sex2);

        List<String> list = new ArrayList<>();
        list.add("JAVA");
        list.add("安卓");
        list.add("IOS");
        list.add("大数据");
        list.add("C++");
        model.addAttribute("list",list);
        Map<String,Object> map = new HashMap<>();
        map.put("name","luyong");
        map.put("sex","男");
        map.put("age",18);
        model.addAttribute("map",map);
        //request.setAttribute("session","11111111111111111111111");
        //request.getSession().setAttribute("userToken","qwqwqwqwqw12121212");

        //开始静态化
        Context context = new Context();
        context.setVariable("there","luyong");
        context.setVariable("birthday",birthday);
        context.setVariable("sex",sex);
        context.setVariable("sex2",sex2);
        context.setVariable("list",list);
        context.setVariable("map",map);
        //定义html生成目录
        String path = "D:/springboot";
        FileWriter out = new FileWriter(path + "/teacher.html");
        templateEngine.process("teacher",context,out);
        out.close();
        return "ok";
    }
}
